<template>
  <div class="sidebar">
    <el-menu
      class="sidebar-el-menu"
      :default-active="onRoutes"
      :collapse="sidebar.collapse"
      background-color="#324157"
      text-color="#bfcbd9"
      active-text-color="#20a0ff"
      unique-opened
      router
    >
      <template v-for="item in items">
        <!-- 父权限 -->
        <template v-if="item.children">
          <el-sub-menu :index="item.path" :key="item.path">
            <template #title>
              <!--  -->
              <el-icon>
                <component :is="item.icon"></component>
              </el-icon>
              <span>{{ item.pname }}</span>
            </template>

            <!-- 子权限 -->
            <template v-for="subItem in item.children">
              <el-sub-menu
                v-if="subItem.children"
                :index="subItem.path"
                :key="subItem.path"
              >
                <template #title>{{ subItem.pname }}</template>
                <el-menu-item
                  v-for="(threeItem, i) in subItem.children"
                  :key="i"
                  :index="threeItem.path"
                >
                  {{ threeItem.pname }}
                </el-menu-item>
              </el-sub-menu>
              <el-menu-item v-else :index="subItem.path">
                {{ subItem.pname }}
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <!-- 父权限 -->
        <template v-else>
          <el-menu-item :index="item.path" :key="item.path">
            <el-icon>
              <component :is="item.icon"></component>
            </el-icon>
            <template #title>{{ item.pname }}</template>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts">
export default {
  created() {
    // alert("正在执行请求");
    var userJSON = localStorage.getItem('user');
    // 解析JSON字符串为JavaScript对象
    var user = JSON.parse(userJSON);

    var token =  localStorage.getItem("token");
    // console.log(user);
    // console.log(user.rid);
    // console.log(token);
    

    this.$axios({
      url: "admin/permission/selectPermissionByPidList",
      methods: "post",
      params: {
        rid: user.rid,
        //rid: 14,
        type: 1,
      },
    }).then((res) => {
      console.log("-----查询出来的当前权限的树-------");
      // pid,icon,path,pname,ppid,children
      console.log(res.data);
      this.items = res.data;
    });
  },
  data() {
    return {
      items: [],
      pidList: [],
    };
  },
};
</script>

<script setup lang="ts">
import { computed, reactive } from "vue";
import { useSidebarStore } from "../store/sidebar";
import { useRoute } from "vue-router";
	const it = [
		{
			icon: 'Odometer',
			index: '/dashboard',
			title: '系统首页',
		},
		{
			icon: 'Calendar',
			index: '1',
			title: '表格相关',
			subs: [
				{
					index: '/table',
					title: '常用表格',
				},
				{
					index: '/import',
					title: '导入Excel',
				},
				{
					index: '/export',
					title: '导出Excel',
				},
			],
		},
		{
			icon: 'DocumentCopy',
			index: '/tabs',
			title: 'tab选项卡',
		},
		{
			icon: 'Edit',
			index: '3',
			title: '表单相关',
			subs: [
				{
					index: '/form',
					title: '基本表单',
				},
				{
					index: '/upload',
					title: '文件上传',
				},
				{
					index: '4',
					title: '三级菜单',
					subs: [
						{
							index: '/editor',
							title: '富文本编辑器',
						},
						{
							index: '/markdown',
							title: 'markdown编辑器',
						},
					],
				},
			],
		},
		{
			icon: 'Setting',
			index: '/icon',
			title: '自定义图标',
		},
		{
			icon: 'PieChart',
			index: '/charts',
			title: 'schart图表',
		},
		{
			icon: 'Warning',
			index: '11',
			title: '账号管理',
			subs: [
				{
					index: '/customer',
					title: '用户管理',
				},
				{
					index: '/role',
					title: '角色管理',
				},
				{
					index: '/permission',
					title: '权限管理',
				},
				{
					index: '/role2',
					title: '角色管理2',
				}]
    }
  ];

const route = useRoute();
const onRoutes = computed(() => {
  // 切换导航栏的时候触发
  // 发送请求
  // items.value = res.data;
  return route.path;
});


	const sidebar = useSidebarStore();
</script>

<style scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}

.sidebar::-webkit-scrollbar {
  width: 0;
}

.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}

.sidebar > ul {
  height: 100%;
}
</style>
